use std::sync::{Arc, Mutex};
use std::thread;

use crate::gen_title;

pub fn ch4_16_1() {
    gen_title("ch4_16_1", 18);

    #[derive(Debug)]
    struct MyBox(*mut u8);

    unsafe impl Send for MyBox {}

    let p = MyBox(5 as *mut u8);
    let t = thread::spawn(move || {
        println!("{:?}", p);
    });

    t.join().unwrap();
}

pub fn ch4_16_2() {
    gen_title("ch4_16_2", 18);

    #[derive(Debug)]
    struct MyBox(*const u8);

    unsafe impl Send for MyBox {}

    unsafe impl Sync for MyBox {}

    let b = &MyBox(5 as *const u8);
    let v = Arc::new(Mutex::new(b));
    let t = thread::spawn(move || {
        let _v1 = v.lock().unwrap();
        println!("{:?}", _v1);
    });

    t.join().unwrap();
}
